﻿using System.Threading.Tasks;
using Red.Controls.ViewModels;

namespace Red.Controls.Diagrams.InteropUtils
{
    class InteropPropertyInitializer
    {
        #region Data

        private readonly string _propertyName;
        private readonly DataItem _parentDataItem;
        private readonly string _fullPath;

        #endregion Data

        #region C-tor

        public InteropPropertyInitializer( string propertyName, DataItem parentDataItem, string fullPath = null )
        {
            _propertyName = propertyName;
            _parentDataItem = parentDataItem;
            _fullPath = fullPath;
        }

        #endregion C-tor

        #region Public methods

        public async Task<DataItem> Execute()
        {
            var path = _propertyName;
            if ( !string.IsNullOrEmpty( _fullPath ) )
                path = _fullPath;

            var subItem = await _parentDataItem.DigUpSubItemAsync( path );
            // when we refresh object from notifier callback, subItem is not updated yet so we need to manually fetch and refresh it.
            await subItem.RefreshAsync();
            return subItem;
        }

        #endregion Public methods
    }
}
